package ru.st.selenium.pages;

import static org.openqa.selenium.support.ui.ExpectedConditions.presenceOfElementLocated;

import org.openqa.selenium.By;
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.FindBy;

public class HomePage extends InternalPage {

	public HomePage(PageManager pages) {
		super(pages);
	}

	@FindBy(xpath = "//div[@id='content']/section/nav/ul/li/div/div/a/img")
	private WebElement AddMovieButton;

	@FindBy(xpath = "//div[@id='content']/section/nav/ul/li[2]/div/div/a/img")
	private WebElement UpdateAllButton;

	@FindBy(xpath = "//div[@id='content']/section/nav/ul/li[3]/div/div/a/img")
	private WebElement ExportButton;

	@FindBy(id = "q")
	private WebElement SearchField;

	public void clickAddMovieButton() {
		AddMovieButton.click();
	}

	public HomePage setSearchValue(String text) {
		SearchField.sendKeys(text);
		SearchField.sendKeys(Keys.ENTER);
		return this;
	}

	public HomePage SelectFilm(String text) {
		driver.findElement(By.xpath(("//img[@alt='New Movie']"))).click();
		return this;
	}

	public HomePage ensurePageLoaded() {
		super.ensurePageLoaded();
		wait.until(presenceOfElementLocated(By
				.xpath("//div[@id='content']/section/nav/ul/li/div/div/a/img")));
		wait.until(presenceOfElementLocated(By.id("q")));
		return this;
	}
}
